package BasicEntity
{
	import adobe.utils.CustomActions;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	import flash.net.URLRequest;
	import flash.text.engine.TextBlock;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	import flash.utils.getTimer;
	public class GameWorld
	{
		public static var  MainPage:Sprite;
		public static var FrameCount:int;
		public static var Tick:int;
		public static var ImgPool:Vector.<BitmapData>;
		public static var NextID:int = 1000;
		public static var GraphicsTool:Graphics;
		
		
		public static var force:Vector2D = new Vector2D();
		public static var Entities:Dictionary;
		
		private static var imgUrlLoadList:Vector.<String>
		private static var end:int;
		
		public static var Obstacles:Dictionary;
		
		public function GameWorld()
		{
			
		}
		
		public static function Init(_mainPage:Sprite,_graphics:Graphics):void
		{
			ImgPool = new Vector.<BitmapData>();
			imgUrlLoadList = new Vector.<String>();
			imgUrlLoadList.push("../imgs/tank02e.png");
			//imgUrlLoadList.push("../imgs/car01-2.png");
			LoadInit();
			MainPage = _mainPage; 
			Entities = new Dictionary();	
			GraphicsTool = _graphics;
			
			Obstacles = new Dictionary();
		}
		
		public static function Start():void
		{		
			end = getTimer();
			MainPage.addEventListener(Event.ENTER_FRAME, function(e:Event):void
			{
				Update();
			});
		}
		/**
		 * _pos:位置 _velicity:速度 _head:朝向 _mass:质量 _maxSpeed:最大速度 _maxTurnRate:最大转速 _maxForce:最大受力
		 * _offsetX:偏移 X _offsetY:偏移 Y  _scale:缩放
		 */
		public static function AddVehicle(_pos:Vector2D,
		                                  _velocity:Vector2D,
								          _head:Vector2D,
								          _mass:Number,
								          _maxSpeed:Number,
								          _maxTurnRate:Number,
								          _maxForce:Number,
								          _offsetX:int,
								          _offsetY:int,
								          _scale:Number):Vehicle
		{
			var vehicleIn:Vehicle = new Vehicle(_pos, _velocity, _head, _mass, _maxSpeed, _maxTurnRate, _maxForce, _offsetX, _offsetY, _scale);
			MainPage.addChild(vehicleIn.Frame);
			vehicleIn.Pos.X = _pos.X;
			vehicleIn.Pos.Y = _pos.Y;			
			
			Entities[vehicleIn.ID] = vehicleIn;
			return vehicleIn;
		}	
		
		public static function AddHunter(_pos:Vector2D,
		                                  _velocity:Vector2D,
								          _head:Vector2D,
								          _mass:Number,
								          _maxSpeed:Number,
								          _maxTurnRate:Number,
								          _maxForce:Number,
								          _offsetX:int,
								          _offsetY:int,
								          _scale:Number):Hunter
		{
			var hunter:Hunter = new Hunter(_pos, _velocity, _head, _mass, _maxSpeed, _maxTurnRate, _maxForce, _offsetX, _offsetY, _scale);
			MainPage.addChild(hunter.Frame);
			hunter.Pos.X = _pos.X;
			hunter.Pos.Y = _pos.Y;				
			Entities[hunter.ID] = hunter;
			return hunter;
		}		
		
		public static function InputKey(_key:int):void
		{
			for each(var item:BaseGameEntity in Entities)
			{
				(item as Vehicle).Steer.t = _key;
			}
		}		
		
		
		
		public static function AddObstacle(_x:Number,_y:Number,_r:Number):void
		{
			var obstacle:Obstacle = new Obstacle(_x, _y, _r);
			GraphicsTool.drawCircle(_x, _y, _r);
			GraphicsTool.beginFill(Math.random() * 255, 1);
			var id:TextField = new TextField();
			id.text =  obstacle.ID.toString();
			MainPage.addChild(id);
			id.x = _x - _r/2;
			id.y = _y - _r/2 +5;
			id.textColor = 0xffffff;
		}
		
		public static function WorldToLocal(_global:Vector2D,_localCoordinatorOrigin:Vector2D,_localCoordinatorHead:Vector2D):Vector2D
		{
			var local:Vector2D =  new Vector2D(_global.X - _localCoordinatorOrigin.X, _global.Y - _localCoordinatorOrigin.Y);
			var angle:Number = local.Angle - _localCoordinatorHead.Angle;
			var re:Vector2D = new Vector2D(0, 0);
			re.Length = local.Length;
			re.Angle = angle;
			return re;
		}
		
		public static function LocalToWorld(_local:Vector2D, _localCoordinatorOrigin:Vector2D, _localCoordinatorHead:Vector2D):Vector2D
		{
			var v:Vector2D = _local.Clone();
			v.Angle = _local.Angle + _localCoordinatorHead.Angle;
			v.X = v.X + _localCoordinatorOrigin.X;
			v.Y = v.Y + _localCoordinatorOrigin.Y;
			return v;
		}
		
		
		
		public static function MatrixToWorld(_local:Vector2D, _localCoordinatorOrigin:Vector2D, _localCoordinatorHead:Vector2D):Vector2D
	    {
			var matrix:Matrix = new Matrix(); 
			return new Vector2D();
		}
		
		private static function LoadInit():void
		{
			if (imgUrlLoadList.length > 0)
			{
			   var st:String = imgUrlLoadList.shift();
			   var load:Loader =  new Loader();
			   load.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void
			   {				
			   	  var l:Loader = e.target.loader as Loader;
			   	  var bmpData:BitmapData = (l.content as Bitmap).bitmapData;
			   	  ImgPool.push(bmpData);	
				  LoadInit();
			   });
			   load.load(new URLRequest(st));
			}
			else
			{
				Start();
				var q:Vehicle = AddVehicle(new Vector2D(400, 300), new Vector2D(0, 0), new Vector2D(), 1000, 1, 1, 1, 65, 56,1);
				q.Heading = new Vector2D(45, 45);
				q.Heading.Angle = 60 * Math.PI / 180;
				var q1:Vehicle = AddVehicle(new Vector2D(600, 400), new Vector2D(0, 0), new Vector2D(), 1000, 1, 1, 1, 65, 56, .7);
				q1.Heading = new Vector2D(45, 45);
				q1.Heading.Angle = -60 * Math.PI / 180;

				AddObstacle(400, 500, 10);
				AddObstacle(300, 200, 10);
				AddObstacle(100, 450, 10);
				AddObstacle(300, 500, 10);
				AddObstacle(500, 100, 10);
				AddObstacle(550, 400, 10);
 
			}
		}
		
		private static function Update():void
		{
			FrameCount++;
			Tick = getTimer() - end;
			end  = getTimer();	
			for each(var item:BaseGameEntity in Entities)
			{
				item.Update(Tick);
			}
		}
		
		
		
		
	}
}